/// <reference path="MicrosoftAjax.js"/>
/// <reference path="Silverlight.js"/>
bluePlayer = function(medialist, mode) 
{   
   this.mode = mode;
   this.medialist= medialist;
   Silverlight.createObjectEx( {   source: 'Players/bluePlayer.xaml', 
                                   parentElement: document.getElementById("SilverlightControlHost"), 
                                   id: "SilverlightControl", 
                                   properties:{ width:'100%', height:'100%', version:'1.0', background:'transparent', isWindowless:'true' }, 
                                   events:{ onLoad:Function.createDelegate(this, this.handleLoad) } } );   
}

bluePlayer.prototype =
{
    playMedia: function(medialist, mode)
    {                
        //Video : mode = 1 
        //Image : mode = 2
        //Snapshot : mode = 3
        if (medialist != undefined)
        {
            this.mediasTab = medialist.split(";");
            this.currentMedia = -1;
        }
        else if(!this.mediasTab)
        {
            this.mediasTab = new Array();
            this.currentMedia = -1;
        }       
        if (this.mode == 1)
        {         
            // this.control.content.findName('EntreePlayer').Begin();
            this.control.content.findName("Image").Visibility = "Collapsed";
            this.control.content.findName("MainImage").Visibility = "Collapsed";
            this.control.content.findName("MainClip").Visibility = "Visible";
            this.control.content.findName("NbrePhotosTextBlock").Visibility = "Collapsed";
            this.nextVideo(null, null);
        }
        if (this.mode == 2)
        {
            if (this.player != undefined)
                this.player.stop();
            this.control.content.findName('EntreePlayerPhoto').Begin();
            this.control.content.findName("MainClip").Visibility = "Collapsed";
            this.control.content.findName("Image").Visibility = "Visible";
            this.control.content.findName("MainImage").Visibility = "Visible";
            this.control.content.findName("NbrePhotosTextBlock").Visibility = "Visible";
            this.nextImage(null, null);
        }
        if (this.mode == 3) 
        {
            this.control.content.findName("MainClip").Visibility = "Collapsed";
            this.control.content.findName("Image").Visibility = "Visible";
            this.control.content.findName("MainImage").Visibility = "Visible";
        }
            
    },
	handleLoad: function(control, userContext, rootElement) 
	{	    
		this.control = control;
	
	    //Video
	    this.control.content.findName('Video').addEventListener("MouseLeave",Function.createDelegate(this,this.videoMouseLeave));
	    this.control.content.findName('Video').addEventListener("MouseEnter",Function.createDelegate(this,this.videoMouseEnter));
	    this.control.content.findName('NextVideoButton').addEventListener("MouseLeftButtonDown",Function.createDelegate(this,this.nextVideo));
	    this.control.content.findName('PreviousVideoButton').addEventListener("MouseLeftButtonDown",Function.createDelegate(this,this.prevVideo));
	    //this.control.content.findName('EntreePlayer').Begin();
	    //this.control.content.findName('EntreePlayer').addEventListener("completed",Function.createDelegate(this,this.constructionStoryBoardCompleted));
		
	    //Images
	    this.control.content.findName('nextButton').addEventListener("MouseEnter",Function.createDelegate(this,this.nextButtonEnter));
	    this.control.content.findName('nextButton').addEventListener("MouseLeftButtonDown",Function.createDelegate(this,this.nextImage));
	    this.control.content.findName('prevButton').addEventListener("MouseEnter",Function.createDelegate(this,this.prevButtonEnter));
	    this.control.content.findName('prevButton').addEventListener("MouseLeftButtonDown",Function.createDelegate(this,this.prevImage));
	    this.control.content.findName('nextButton').addEventListener("MouseLeave",Function.createDelegate(this,this.nextButtonLeave));
	    this.control.content.findName('prevButton').addEventListener("MouseLeave",Function.createDelegate(this,this.prevButtonLeave));
	    this.control.content.findName('MainImage').addEventListener("DownloadProgressChanged", Function.createDelegate(this,this.onImageDownloadProgressChanged));
	    
	    this.control.content.findName('MaskChangeStep1').addEventListener("completed",Function.createDelegate(this,this.endChangeImageStep1));
	    
	    this.descriptionTxt = this.control.content.findName('tbDescriptifTextBlock');
	    //this.descriptionTitle = this.control.content.findName('VideoDescriptionTitle');
        this.currentMediaDescription = null;
    
        this.proxy = new BluePortal.QuikStart.WSAjax.WSPlayer();        
        this.playMedia (this.medialist, this.mode);
	},
	 get_quality : function()
    {
        return this.videoQuality;
    },
    set_quality : function (value)
    {
        if(value != this.videoQuality)
        {
            this.videoQuality = value;
            if(this.mode == 1)
            {
                if(value == videoQuality.high && this.currentMediaDescription.HDUrl)
                    this.player.set_mediaUrl(this.currentMediaDescription.HDUrl);
                else
                    this.player.set_mediaUrl(this.currentMediaDescription.Url);
            }
        }
    },
	endChangeImageStep1: function(sender, aventArgs)
	{
        //window.setTimeout(Function.createDelegate(this,this.maskChangeVisibility),500);   
	},
	nextImage: function(sender, eventArgs)
	{
	    this.currentMedia++;
	    this.mediaID =  this.mediasTab[this.currentMedia];
	    this.descriptionTxt.Text="Loading...";
	    //this.descriptionTitle.Text ="Loading...";
	    //this.proxy.GetMediaDescription(this.mediasTab[this.currentMedia],"",Function.createDelegate(this,this.setMediaInfo));
	    this.control.content.findName("MainImage").Source = "Images/Image.ashx?vid=" + this.mediasTab[this.currentMedia];	    
        this.control.content.findName("MaskChangeStep1").begin();	    
        this.nextprevVisibility();
	},
	prevImage: function(sender, eventArgs)
	{
	    this.currentMedia--;
	    this.mediaID =  this.mediasTab[this.currentMedia];
	    this.descriptionTxt.Text="Loading...";
	    //this.descriptionTitle.Text ="Loading...";
	    //this.proxy.GetMediaDescription(this.mediasTab[this.currentMedia],"",Function.createDelegate(this,this.setMediaInfo));
        this.control.content.findName("MainImage").Source = "Images/Image.ashx?vid=" + this.mediasTab[this.currentMedia];	    
        this.control.content.findName("MaskChangeStep1").begin();	 
        this.nextprevVisibility();
	},
	nextVideo: function (sender, eventArgs)
	{	    
	    this.currentMedia++;
	    if(this.mediasTab && this.mediasTab.length > this.currentMedia)
	    {
	        if(this.mediasTab[this.currentMedia].length == 0)
	            this.nextVideo(sender,eventArgs);
	         else
	         {
	            this.mediaID =  this.mediasTab[this.currentMedia];
	        }
	    }
	    this.proxy.GetVideoUrl (this.mediaID, 0, Function.createDelegate(this, this.endGetVideoUrl));
        this.nextprevVisibility();
	},
	prevVideo: function (dender, envetArgs)
	{
	    this.currentMedia--;
	    if(this.mediasTab && this.mediasTab.length > this.currentMedia)
	    {
	        this.mediaID =  this.mediasTab[this.currentMedia];
	    }
	    this.proxy.GetVideoUrl (this.mediaID, 0, Function.createDelegate(this, this.endGetVideoUrl));
        this.nextprevVisibility();
	},
	endGetVideoUrl: function(result)
	{
	    //alert (result);
	    if(result)
	    {
	        if (this.player == undefined)
	        {	            
	            this.player = $create(Sys.Preview.UI.Xaml.Media.Player, {"mediaUrl":result,"volume":0.76}, null, null, $get("SilverlightControl"));	        
	        }
	        else
	        {	        
                this.player.set_mediaUrl(result);
	        }
	    }
	},
	onImageDownloadProgressChanged: function(sender, eventArgs)
    {
        if (sender.downloadProgress == 1)
        {
	        this.control.content.findName('EntreePlayerPhoto').Begin();
	        this.control.content.findName("NbrePhotosTextBlock").Text = (this.currentMedia + 1)  + "/" + (this.mediasTab.length);   
	    }
    },
	nextprevVisibility: function(sender, eventArgs)
	{
	    if (this.currentMedia <= 0)
	    {
	        this.control.content.findName("prevButton").Visibility = "Collapsed";
	        this.control.content.findName("PreviousVideoButton").Visibility = "Collapsed";
	    }
	    else
	    {
	        this.control.content.findName("prevButton").Visibility = "Visible";
	        this.control.content.findName("PreviousVideoButton").Visibility = "Visible";
	    }
	    if (!this.mediasTab || this.currentMedia >= this.mediasTab.length-1)
	    {
	        this.control.content.findName("nextButton").Visibility = "Collapsed";
	        this.control.content.findName("NextVideoButton").Visibility = "Collapsed";
	    }
	    else
	    {
	        this.control.content.findName("nextButton").Visibility = "Visible";
	        this.control.content.findName("NextVideoButton").Visibility = "Visible";
	    }
	},
	__playVideoUrl : function (url) 
	{
        //this.control.content.findName("tbDescriptifTextBlock").Text = url;
	    if (this.player == undefined)
	    {
	        this.player = $create(Sys.Preview.UI.Xaml.Media.Player, {"mediaUrl":url,"volume":0.76}, null, null, $get("profrancePlayer1"));	        
	    }
	    else
	    {	        
	        this.player.set_mediaUrl(url);
	    }  
	},
	
	prevButtonEnter: function(sender, eventArgs)
	{
	    this.control.content.findName('prevButtonEnter').Begin();
	},
	nextButtonEnter: function(sender, eventArgs)
	{
	    this.control.content.findName('nextButtonEnter').Begin();
	},
	prevButtonLeave: function(sender, eventArgs)
	{
	    this.control.content.findName('prevButtonLeave').Begin();
	},
	nextButtonLeave: function(sender, eventArgs)
	{
	    this.control.content.findName('nextButtonLeave').Begin();
	},
    videoMouseLeave: function(sender, eventArgs)
	{
	    if(this.mode == 1)
	        this.control.content.findName('FermeturePlayer').Begin();
	},
    videoMouseEnter: function(sender, eventArgs)
	{
	   if(this.mode == 1)
	        this.control.content.findName('EntreePlayer').Begin();
	},
	constructionStoryBoardCompleted: function (sender, eventArgs)
	{


	}
}